import { createRouter, createWebHistory } from 'vue-router'
import post from '@/lib/post'  //引入post
import TypeScript from '../pages/typescript/learn-typescript.vue'
import Page1 from '../pages/homework/page-1.vue'
import Page2 from '../pages/homework/page-2.vue'
import Page3 from '../pages/homework/page-3.vue'

import Login from '../pages/login/LoginIndex.vue'   //登录页面
import ForgetPassword from '../pages/login/forgetPassword.vue'   //忘记密码页面


import Main from '../pages/homework/MainPage.vue'   //二级路由主页面

import Cart from '../pages/cart/cartIndex.vue'  //查看购物车列表


import Organization from '../pages/organization/OrganizationIndex.vue'  //组织列表
import OrganizationPopup from '../pages/organization/organizationPopup.vue'  //新增组织弹出框组件

import Member from '../pages/member/MemberIndex.vue'  //人员列表
import Detail from '../pages/member/DetailIndex.vue'  //新增人员弹出框组件

import Goods from '../pages/goods/goodsIndex.vue'  //商品列表
import GoodsPopup from '../pages/goods/goodsPopup.vue'  //新增商品弹出框组件


import Role from '../pages/role/roleIndex.vue'  //角色列表
import RolePopup from '../pages/role/rolePopup.vue'  //新增角色弹出框组件

import Station from '../pages/station/stationIndex.vue'  //自提点列表
import StationPopup from '../pages/station/stationPopup.vue'  //新增自提点弹出框组件

import Setting from '../pages/setting/SettingIndex.vue'  //设置列表



import Home from '../pages/home/index.vue'  //设置首页




 const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/login',
      component:Login,
      meta:{notNeedLogin:true} //是否不需要登录
    },{
      path:'/forgetPassword',
      component:ForgetPassword,
      meta:{notNeedLogin:true} //是否不需要登录
    },{
      path:'/typescript',
      component:TypeScript,
    },
    {
      path:'/',
      component:Main,
      redirect:'/home',
      children: [
        {
          path:'/page-1',
      component:Page1,
        },{
          path:'/page-2',
      component:Page2,
        },{
          path:'/page-3',
      component:Page3,
        },{
          path:'/member',
      component:Member,
        },{
          path:'/detail',
      component:Detail,
        },{
          path:'cart',     //购物车
          component:Cart   
        },{
          path:'station',     //自提点
          component:Station    
        },{
          path:'stationpopup',
          component:StationPopup
        },{
          path:'goods',     //商品
          component:Goods    
        },{
          path:'goodspopup',
          component:GoodsPopup
        },{
          path:'/organization',
          component:Organization,
        },{
          path:'/organizationpopup',
          component:OrganizationPopup,
        },{
          path:'/role',
      component:Role,
        },{
          path:'/rolepopup',
      component:RolePopup,
        },{
          path:'/home',
      component:Home,
        },{
          path:'/setting',
          component:Setting,  
        },{
          path:'/canvas', //画布
          component:()=>import('@/pages/canvas/main.vue'),  
          redirect:'canvas/hello',
          children:[{
            path:'/canvas/hello', //画布
            component:()=>import('@/pages/canvas/helloworld.vue'),  
          },{
            path:'/canvas/triangle', //三角形
            component:()=>import('@/pages/canvas/triangle.vue'),  
          },{
            path:'/canvas/image', //图像
            component:()=>import('@/pages/canvas/image.vue'),  
          },{
            path:'/canvas/game', //游戏
            component:()=>import('@/pages/canvas/game.vue'),  
          },{
            path:'/canvas/fabric', //
            component:()=>import('@/pages/canvas/fabric.vue'),  
          },{
            path:'/canvas/three', //three
            component:()=>import('@/pages/canvas/three.vue'),  
          }]
        }
      ]
    }
  ]
})


//全局前置路由守卫判断登录情况
router.beforeEach(async(to,from,next)=>{
  if(to.meta.notNeedLogin){   
    //如果不需要登录的页面
    return next()
  }else{
    // 需要登录的页面
    if(localStorage.user){  //登录过
      const data:any =await post('/detail/member',{id:JSON.parse(localStorage.user).user.userId})
      if(data.success){  //获取个人信息
        return next()
      }else{
        next('/login') //跳转至登录页
        
        console.log(1);
        console.log(localStorage.user);

        
      }
    }else{
      next('/login')  //跳转至登录页
      console.log(2);
      
    }
  }
})


export default router
